*Creates paper Figure 4 and table 2

*cd "YOUR DIRECTORY HERE"

********************************************************
*	1. Construct Bm estimates if all new men vote left. 
*	2. Generate Figure 4
*	3. Generate Table 2
********************************************************



********************************
*1. Construct Bm estimates if all new men vote left
************************************
use "simulation.dta", clear
keep if year<=1924
	
	
	*construct a measure of men's support for the left assuming the old male voters voted like their most left year and the new men voted 100% for the left when possible. 
		egen TVleft=rowtotal(COM SOC LIB) /*Actual total number of votes cast for the left */
		recode TVleft (.=0) 
		
		*Old men's # left votes if they vote like their most left year 
		gen TVleft_oldmax= maxleft*VT_old if year==1921 
		recode TVleft_oldmax (.=0) 
		assert TVleft_oldmax<=VT_old if year==1921 & TVleft!=. 
		
		
		*if old men are at their max, and all new men vote left. The proportion of men that vote left: 
		gen Bm_max1=[(TVleft_oldmax) + (1*VT_new)] / VTmen if year==1921

		
		foreach Bm in Bm_max1 {

			gen Bw_m`Bm'= (left - `Bm'*(1-womensharecast ))  / womensharecast if year==1921
		
			*substitute with realistic values 
			replace Bw_m`Bm'=1 if Bw_m`Bm'>=1 & Bw_m`Bm'!=.
			replace Bw_m`Bm'=0 if Bw_m`Bm'<=0 & Bw_m`Bm'!=.
		
		}
	
		la var Bw_mBm_max1 "Women | Old men most left, all new men left"
		
save "simulation2.dta", replace

*************************************************	
*2:  Fig 4: Graph of women's leftism with Duncan-Davis bounds and predictions based on all men acting like most left year, or with all new men being left. 
*************************************************
	
	*Create deciles of left support in 1921 		
	xtile LeftDecile=left if year==1921, nq(10)
	_pctile left if year==1921, p(10(10)99)
	return list 
	lab define decile 1 "Most Conservative Decile" 10 "Most Leftist Decile"
	la values LeftDecile decile
	tab LeftDecile
		
	cap gen pos=-.5   /*code for the rug plot */ 
	cap gen pipe = "|"
	
	
	forvalues n = 4(4)12 {
	local xlab `xlab' `n' "`=string(round(exp(`n'),10),"%9.0fc")'"
}

	# delimit ; 
	
	twoway 
		(lowess Bw_m0 lElect, bwidth(.3)lcolor(gs12) lwidth(thick) lpattern(solid) )
		(lowess Bw_mmax lElect, bwidth(.3)lcolor(red) lwidth(thick) lpattern(solid) )
		(lowess Bw_mBm_max1 lElect, bwidth(.3)lcolor(pink) lwidth(thick) lpattern(solid) )
		(lowess Bw_m1 lElect , bwidth(.3)lcolor(gs12) lwidth(thick) lpattern(solid) ) 
		(scatter pos lElect,   ms(none) mcolor(gs16) mla(pipe) mlabpos(0))
		
		if year==1921 & red21==0
		
			, by(LeftDecile, note("") legend(off) style(compact)) 
			ytitle("Women's Predicted Left Vote Share", size(large)) 
			xtitle("Electorate size",  size(large) )  
			ysca(titlegap(*10)) xsca(titlegap(*10)) 
			xlab(`xlab') 
			scheme(tufte) 
			 
				; 
			# delimit cr 
			
		
			graph export "./images/fig4.eps", replace 	
			/*	
				legend( col(1)
				label(1 "No Men Vote Left")
				label(2 "All Men Most Left Year")
				label(3 "All New Men Left")
				label(4 "All Men Vote Left")
				order(1 2 3 4))
			*/
					
	
		
		

**********************************************
*3: Table 2 weighted averages 
**********************************************		
		
		*Weighted 
	
		foreach var in Bw_m0 Bw_m1 Bw_mmax  {
	
	
		asgen W_`var'=`var' if year==1921, weight(VTw)
		la var W_`var' "Weighted `var'" 
		asgen W_`var'_urban=`var' if CityTown==1 & year==1921, weight(VTw)
		la var W_`var'_urban "Weighted `var', Urban" 
		asgen W_`var'_rural=`var' if CityTown==0 & year==1921, weight(VTw)
		la var W_`var'_rural "Weighted `var' , Rural" 
		
		}
	
	
		**Averages in the paper 
		bys City: sum Bw_mL1  if year==1921 & red21==0
		ttest Bw_mL1 if year==1921 & red21!=1, by(City)
		
		bys City: sum Bw_mL2 if year==1921 & red21==0
		ttest Bw_mL2  if year==1921 & red21!=1, by(City)
		
		bys City: sum Bw_mmax  if year==1921 & red21==0
		ttest Bw_mmax  if year==1921 & red21!=1, by(City)
		
		sum  Bw* 
		
			
		*Fact cited in text: Back of the envelope. 
		
		preserve
		collapse (sum) VTw VTm SOC LIB COM (firstnm) W_Bw_mmax W_Bw_mmax_urban W_Bw_mmax_rural if year==1921, by(City)
		gen TVleft=SOC+LIB+COM
		gen TVWleft_urban=VTw*W_Bw_mmax_urban if City==1
		gen TVWleft_rural=VTw*W_Bw_mmax_rural if City==0
		
		gen WshareLeftUrban=TVWleft_urban/TVleft if City==1
		gen WshareLeftRural=TVWleft_rural/TVleft if City==0
		
		list WshareLeftUrban WshareLeftRural
		
		restore 
		
		*Rural areas women contributing 33.56% of votes to the left. Urban areas they are contributing 46.9% of the votes for the left.		
		
		
		
